package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 聊天室客户端
 *
 */
public class Client {
    private Socket socket;

    /*
    构造器,用于初始化客户端
     */
    public Client(){
        try {
            System.out.println("正在链接服务器...");
            /*
                Socket实例化就是与服务器建立链接的过程,此时需要传入两个
                参数
                参数1:服务端的IP地址，用于找到服务端的计算机
                参数2:服务端口,用于找到服务器程序
             */
            socket = new Socket("127.0.0.1",8088);
//            socket = new Socket("176.223.9.153",8089);
            System.out.println("与服务器成功链接!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
    用于客户端开始工作的方法
     */
    public void start(){
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);

            Scanner scan = new Scanner(System.in);
            //首先要求用户输入一个昵称
            String nickname = "";
            while (true){
                System.out.println("请输入昵称:");
                nickname = scan.nextLine();
                if (nickname.trim().length()>0){
                    pw.println(nickname);
                    break;
                }
            }

            //将接受服务端发送过来消息的线程启动
            ServerHandler handler = new ServerHandler();
            Thread t = new Thread(handler);
            t.setDaemon(true);
            t.start();

            System.out.println("开始聊天吧!");
            while (true){
                System.out.println("请输入信息:");
                String line = scan.nextLine();
                if ("exit".equals(line)){
                    break;
                }
//                pw.println("你好!服务端!");
                pw.println(line);
            }

//            out.write(2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //socket的close方法会进行四次挥手
                //并且也会关闭通过socket获取的输入流和输出流
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //实际开发中不会在main方法中写业务逻辑,main方法是静态方法会有诸多不便
        Client client = new Client();//调用构造器初始化客户端
        client.start();//调用start方法使客户端开始工作
    }
    private class ServerHandler implements Runnable{
        @Override
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in,StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ((line=br.readLine())!=null){
                    System.out.println(line);
                }
            } catch (IOException e) {

            }

        }
    }
}
